package com.boomsecret.juc;

import java.util.concurrent.TimeUnit;

/**
 * @author wangjinliang on 2019/1/23.
 */
public class ThreadDemo {
    public static void main(String[] args) {
        Thread[] threads = new Thread[8];
        for (int index = 0; index < 8; index ++) {
            threads[index] = new CustomThread(index);
            threads[index].start();
        }

        for (Thread thread : threads) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("main end.");
    }

    public static class CustomThread extends Thread {
        private int index;

        public CustomThread(int index) {
            this.index = index;
        }

        @Override
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (index % 3 == 0) {
                Thread.yield();
            }
            System.out.println("thread index " + index);
        }
    }
}
